home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Encoder / •Sources / PICTEncode.cp < prev    next >
Encoding:
Text File  |  1997-04-02  |  3.5 KB  |  127 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    PICTEncode.cp
  4. #
  5. #
  6. #    Author: Timothy Carroll
  7. #    Apple Developer Technical Support
  8. #    timc@apple.com
  9. #
  10. #    Modification History: 
  11. #
  12. #    4/2/97        TMC        PICTEncode would double-dispose of the PICT.  I wasn't NULLing
  13. #   out the PICT local after disposing of it explicitly in the loop.  Spotlight found this.
  14. #    2/24/97        TMC        Now explicitly include main.h
  15. #    8/15/96        TMC     Initial Release
  16. #
  17. #
  18. #
  19. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  20. #
  21. #
  22. #    You may incorporate this sample code into your applications without
  23. #    restriction, though the sample code has been provided "AS IS" and the
  24. #    responsibility for its operation is 100% yours.  However, what you are
  25. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  26. #    after having made changes. If you're going to re-distribute the source,
  27. #    we require that you make it clear in the source that the code was
  28. #    descended from Apple Sample Code, but that you've made changes.
  29. #
  30. *************************************************************************************/
  31.  
  32. #include "Main.h"
  33. #include "TGraphic.h"
  34. #include "PictEncode.h"
  35.  
  36.  
  37. OSStatus PICTEncode (short inputFileResNum, short outputFileResNum)
  38. {
  39.     OSStatus        theErr;
  40.  
  41.     UInt16            numPicts, loop;
  42.  
  43.     // we pass these to GetResInfo so that we can get the actual resource ID.
  44.     SInt16            resID;
  45.     ResType            resType;
  46.     Str255            resName;
  47.  
  48.     // Holds the last value on the resource chain since we change the top resource a lot
  49.     SInt16            saveResNum;
  50.  
  51.     // Temporarily holds the resource so we can get information on it
  52.     Handle             pict = NULL;
  53.     TGraphic         *compiled = NULL;
  54.         
  55.     saveResNum = CurResFile();
  56.  
  57.     UseResFile (inputFileResNum);
  58.  
  59.  
  60.     // First thing is to copy the color table resource to the output.
  61.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  62.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  63.  
  64.  
  65.     // get the color table
  66.     gAppColorTable = GetCTable( kAppColorTableResID );
  67.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  68.  
  69.     // determine the number of PICT resources
  70.     numPicts = Count1Resources( 'PICT' );
  71.  
  72.     
  73.     // get each one,
  74.     for( loop = 1; loop <= numPicts; loop++ )
  75.     {
  76.         // load the pict
  77.         UseResFile (inputFileResNum);
  78.                     
  79.         // we'll get the pict first so that we can get the information out of it.
  80.         pict = Get1IndResource( 'PICT', loop );
  81.         theErr = ResError();
  82.         FAIL_NIL (pict, "\pFailed to load the picture")
  83.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  84.             
  85.         // determine its id and name
  86.         GetResInfo( pict, &resID, &resType, resName );
  87.         theErr = ResError();
  88.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  89.         
  90.         ReleaseResource (pict);
  91.         pict = NULL; // We're done with this PICT, NULL it out so we don't double dispose
  92.         
  93.         
  94.         // Okay, we know the PICT's resID, build the compiled graphic and write it to the output file.
  95.         compiled = TGraphic::NewGraphic (resID);
  96.         FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
  97.         
  98.         UseResFile (outputFileResNum);
  99.         
  100.         compiled->WriteToGraphicResource();
  101.         compiled->WriteToPICTResource();
  102.         compiled->DisposeReference();
  103.         
  104.         compiled = NULL; // Make sure we don't double dispose of the compiled sprite
  105.  
  106.     }
  107.     // restore the previous port and device
  108.     
  109.     goto cleanup;
  110.     
  111. error:
  112.     
  113.     if (theErr == noErr)
  114.         theErr = paramErr;
  115. cleanup:
  116.     
  117.     UseResFile (saveResNum);
  118.     
  119.     if (pict)
  120.         ReleaseResource (pict);
  121.     if (compiled)
  122.         compiled->DisposeReference();
  123.     if (gAppColorTable != NULL)
  124.         DisposeCTable (gAppColorTable);
  125.     gAppColorTable = NULL;
  126.     return theErr;
  127. }